﻿Imports Publics
Imports System.Windows.Forms
Imports System.Data.Linq

Public Class FrmDMKho

    Private _LaThoat As Boolean
    Public Property LaThoat() As Boolean
        Get
            Return _LaThoat
        End Get
        Set(ByVal value As Boolean)
            _LaThoat = value
        End Set
    End Property

    Private _IDKho As String
    Public Property IDKho() As String
        Get
            Return _IDKho
        End Get
        Set(ByVal value As String)
            _IDKho = value
        End Set
    End Property

    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private _MaKho As String
    Public Property MaKho() As String
        Get
            Return _MaKho
        End Get
        Set(ByVal value As String)
            _MaKho = value
        End Set
    End Property

    Private Sub FrmKho_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon And grvKho.FocusedRowHandle >= 0 Then
            IDKho = grvKho.GetRowCellValue(grvKho.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If
    End Sub
    Public Shared Them, Sua, Xoa, SaoChep As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSaoChep.Visibility = IIf(SaoChep, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub

    Private Sub FrmKho_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnThemMoi.Caption = BtnAddNewCaption
        btnSuaDoi.Caption = BtnEditCaption
        btnXoa.Caption = BtnDeleteCaption
        btnXuatExcel.Caption = BtnExportDataCaption
        If Not LaChon Then
            btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
        GroupControl1.Text = GetLabelPhimTatCoBan("", True, True, True)

        FillDataToGrid()
        SetFilterConditionMa(grvKho, grdColMaKho, grdColMaKho.FieldName, MaKho, LaChon)
        PhanQuyen()
    End Sub
    Private Sub FillDataToGrid()
        Try
            Dim lstSource = (From ds In dbTV.DM_Khos Where ds.InUsed Select New With { _
                             .ID = ds.ID, .MaKho = ds.MaKho, .TenKho = ds.TenKho}).ToList
            grdKho.DataSource = lstSource
            grdKho.RefreshDataSource()
        Catch ex As Exception
            MsgInfo(ex.Message)
        End Try
    End Sub

    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatKho
        frm.Action = vAddAction
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        frm.ShowDialog()
        FillDataToGrid()

    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        LaThoat = True
        Close()
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid()
    End Sub

    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        If grvKho.FocusedRowHandle < 0 Then
            MsgInfo("Bạn chưa chọn bản ghi để sửa")
            Return
        End If
        Dim strID = grvKho.GetRowCellValue(grvKho.FocusedRowHandle, grdColID).ToString
        Dim objEdit As DM_Kho = (From ds In dbTV.DM_Khos Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If objEdit IsNot Nothing Then
            Dim frm As New FrmCapNhatKho
            frm.KhoEdit = objEdit
            frm.Action = vEditAction
            frm.StartPosition = FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        Else
            MsgInfo("Không tồn tại dữ liệu")
        End If
    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        If grvKho.FocusedRowHandle > -1 Then
            If MsgAskDelete(String.Format("Bạn có muốn xóa bản ghi {0} này không?", grvKho.GetRowCellValue(grvKho.FocusedRowHandle, grdColMaKho).ToString)) = vMsg.vReSult.Yes Then
                Try
                    Dim strID = grvKho.GetRowCellValue(grvKho.FocusedRowHandle, grdColID).ToString()
                    Dim objDelete = (From ds In dbTV.DM_Khos Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
                    If objDelete IsNot Nothing Then
                        objDelete.InUsed = False
                        objDelete.EditedOn = dbTV.GetDateTimeServer
                        iSubmitChange()
                        FillDataToGrid()
                    End If
                Catch ex As Exception
                    MsgInfo(ex.Message)
                End Try
            End If
        End If
    End Sub

    Private Sub btnSaoChep_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSaoChep.ItemClick
        Dim strID = grvKho.GetRowCellValue(grvKho.FocusedRowHandle, grdColID).ToString
        If strID IsNot Nothing Then
            Dim objEdit As DM_Kho = (From ds In dbTV.DM_Khos Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
            Dim frm As New FrmCapNhatKho
            frm.KhoEdit = objEdit
            frm.Action = vCopyAction
            frm.StartPosition = FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        End If
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdKho)
    End Sub
End Class
